package com.firebird.app.kotlin.stock.trend.view

import android.content.Context
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.firebird.app.kotlin.stock.trend.R


/**
 * 自定义可以自定义指示器布局的ViewPager控件
 * @author ljk
 * @date   2019年11月7日
 */
class TouTiaoPagerView : RelativeLayout, OnPageChangeListener {
    private var currentTab = 0
    private var tabImgWidth = 0
    private var img_tab: ImageView
    private var viewPager: ViewPager

    constructor(context: Context?, LayoutId: Int) : super(context) {
        View.inflate(context, LayoutId, this)
        viewPager = findViewById<View>(R.id.vp_fragment) as ViewPager
        img_tab = findViewById<View>(R.id.img_tab) as ImageView
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SimplePagerView)
        val layout = typedArray.getResourceId(R.styleable.SimplePagerView_pager_layout, R.layout.layout_news_toutio)
        typedArray.recycle()
        View.inflate(context, layout, this)
        viewPager = findViewById<View>(R.id.vp_fragment) as ViewPager
        img_tab = findViewById<View>(R.id.img_tab) as ImageView
    }

    fun setOnPageChangeListener(listener: OnPageChangeListener?) {
        viewPager.addOnPageChangeListener(listener!!)
    }

    /**
     * 设置控件中数据
     */
    fun setPagerList(viewlist: List<View>?) {
        viewPager.adapter = SimplePagerAdapter(viewlist)
        //获取屏幕分辨率 宽度
        var dm = DisplayMetrics()
        dm = resources.displayMetrics
        val screenW = dm.widthPixels
        tabImgWidth = if (viewlist != null && viewlist.size > 0) {
            screenW / viewlist.size
        } else {
            screenW
        }
        val para = img_tab.layoutParams as LayoutParams
        para.width = tabImgWidth
        img_tab.layoutParams = para
    }

    /**
     * 设置当前指示器 位置
     * @param position
     */
    private fun setCurrentTab(position: Int) {
        var animation: Animation? = null
        animation = TranslateAnimation((tabImgWidth * currentTab).toFloat(), (tabImgWidth * position).toFloat(), 0f, 0f)
        animation.setFillAfter(true) //True:图片停在动画结束位置
        animation.setDuration(300) //设置动画完成时间
        img_tab.startAnimation(animation)
        currentTab = position
    }

    override fun onPageScrollStateChanged(arg0: Int) {}
    override fun onPageScrolled(arg0: Int, arg1: Float, arg2: Int) {}
    override fun onPageSelected(position: Int) {
        setCurrentTab(position)
    }

    /**
     * 简单的ViewPager适配器
     */
    internal inner class SimplePagerAdapter(private val viewList: List<View>?) : PagerAdapter() {
        override fun getCount(): Int {
            return viewList?.size ?: 0
        }

        override fun isViewFromObject(arg0: View, arg1: Any): Boolean {
            return arg0 === arg1
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(viewList!![position])
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            container.addView(viewList!![position])
            return viewList[position]
        }

    }
}